package light.mvc.service.item.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import light.mvc.dao.BaseDaoI;
import light.mvc.model.item.Ttreatment;
import light.mvc.page.base.PageFilter;
import light.mvc.page.item.Treatment;
import light.mvc.service.item.TreatmentServiceI;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TreatmentServiceImpl implements TreatmentServiceI {

	@Autowired
	private BaseDaoI<Ttreatment> treatmentDao;

	@Override
	public void add(Treatment treatment) {
		Ttreatment t = new Ttreatment();
		BeanUtils.copyProperties(treatment, t);
		treatmentDao.save(t);
	}

	@Override
	public void delete(Integer id) {
		Ttreatment t = treatmentDao.get(Ttreatment.class, id);
		treatmentDao.delete(t);
	}

	@Override
	public void edit(Treatment treatment) {
		Ttreatment t = treatmentDao.get(Ttreatment.class,treatment.getId());
		BeanUtils.copyProperties(treatment, t);
		treatmentDao.update(t);
	}

	@Override
	public Treatment get(Integer id) {
		Ttreatment t = treatmentDao.get(Ttreatment.class,id);
		Treatment treatment = new Treatment();
		BeanUtils.copyProperties(t, treatment);
		
		return treatment;
	}
	
	@Override
	public List<Treatment> dataGrid(Treatment treatmentSome, PageFilter ph) {
		List<Treatment> treatments = new ArrayList<Treatment>();
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = " from Ttreatment t ";
		List<Ttreatment> list = treatmentDao.find(hql + whereHql(treatmentSome, params) + orderHql(ph), params, ph.getPage(), ph.getRows());
		for (Ttreatment t : list) {
			Treatment treatment = new Treatment();
			BeanUtils.copyProperties(t, treatment);
			treatments.add(treatment);
		}
		return treatments;
	}
	
	@Override
	public Long count(Treatment treatment, PageFilter ph) {
		Map<String, Object> params = new HashMap<String, Object>();
		String hql = " from Ttreatment t ";
		return treatmentDao.count("select count(*) " + hql + whereHql(treatment, params), params);
	}
	
	private String whereHql(Treatment treatment, Map<String, Object> params) {
		String hql = "";
		if (treatment != null) {
			hql += " where 1=1 ";
			if (treatment.getTreatmentItem()!= null) {
				hql += " and t.treatmentItem like :treatmentItem";
				params.put("name", "%" + treatment.getTreatmentItem() + "%");
			}
		}
		return hql;
	}

	private String orderHql(PageFilter ph) {
		String orderString = "";
		if ((ph.getSort() != null) && (ph.getOrder() != null)) {
			orderString = " order by t." + ph.getSort() + " " + ph.getOrder();
		}
		return orderString;
	}
	
	@Override
	public Treatment getByItem(Treatment treatmentSome) {
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("treatmentItem", treatmentSome.getTreatmentItem());
		Ttreatment t = treatmentDao.get("from Ttreatment t where t.treatmentItem = :treatmentItem",params);
		Treatment treatment = new Treatment();
		if(t!=null){
			BeanUtils.copyProperties(t, treatment);
		}else{
			return null;
		}
		return treatment;
	}
	
	@Override
	public List<Treatment> list() {
		List<Treatment> treatments = new ArrayList<Treatment>();
		String hql = " from Ttreatment t ";
		List<Ttreatment> list = treatmentDao.find(hql);
		for (Ttreatment t : list) {
			Treatment treatment = new Treatment();
			BeanUtils.copyProperties(t, treatment);
			treatments.add(treatment);
		}
		return treatments;
	}
}
